package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;

/**
 * 中国网滨海高新
 * @author Persh
 *
 */
public class SgxwNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		//"http://[a-z]*.022china.com/[0-9]{4}/[0-9]{2}-[0-9]{2}/[0-9]*_[0-9]*.html"
		"http://news.sgxw.cn/[0-9]*/[0-9]*/[0-9]*.html"
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		//title =doc.select("div#chan_newsBlk h1").text();
		title =doc.select("h1#artical_topic").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("div#artical_real"));

		/*
		 * 作者
		 */
		String authorStr=null;
		authorStr=doc.select("div#artical_sth").text();
		//author=StringUtils.substringBefore(authorStr, "：");
		author=StringUtils.substringBetween(authorStr, "作者：","字号");
		date=DateUtils.matchDate(doc.select("div#artical_sth").text());
		if(date == null) {
			date = DateUtils.matchDate(doc.select("div.user_des,p.atricle_time").text());
		}

		if(StringUtils.isBlank(title)){
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	public static void main(String[] args) {
		SgxwNewsAnalyse analyse=new SgxwNewsAnalyse();
		//String url="http://news.022china.com/2014/01-17/7846_0.html";
		String url="http://news.sgxw.cn/2015/0907/537988.html";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
